{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-20T12:40:23.134541Z",
     "start_time": "2024-09-20T12:40:23.131533Z"
    }
   },
   "cell_type": "code",
   "source": "import pandas as pd ",
   "id": "56def7db99671bf5",
   "outputs": [],
   "execution_count": 1
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-09-20T12:40:23.730009Z",
     "start_time": "2024-09-20T12:40:23.135066Z"
    }
   },
   "source": [
    "train_data = pd.read_csv(r\"D:\\桌面\\天猫复购预测\\data\\data_format1\\train_all_k.csv\")\n",
    "test_data = pd.read_csv(r\"D:\\桌面\\天猫复购预测\\data\\data_format1\\test_all_k.csv\")"
   ],
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "负样本抽样",
   "id": "2d6c2382e8431e39"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-20T12:40:37.570147Z",
     "start_time": "2024-09-20T12:40:37.561902Z"
    }
   },
   "cell_type": "code",
   "source": "train_data['label'].value_counts()",
   "id": "a4798ef21d8e32ba",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "label\n",
       "0.0    241303\n",
       "1.0     15838\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-20T12:46:10.437472Z",
     "start_time": "2024-09-20T12:46:10.433533Z"
    }
   },
   "cell_type": "code",
   "source": "train_data.shape",
   "id": "65e2301a25f78a7c",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(257141, 23)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-20T13:14:24.829575Z",
     "start_time": "2024-09-20T13:14:24.818038Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 统计label列中各个标签的出现次数\n",
    "label_counts = train_data['label'].value_counts()\n",
    "label_counts"
   ],
   "id": "ae3553100f68dbe",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "label\n",
       "0.0    241303\n",
       "1.0     15838\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-20T13:15:52.915152Z",
     "start_time": "2024-09-20T13:15:52.911059Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 获取label为0的样本数量\n",
    "num_samples_to_remove = int(label_counts[0] * 1)\n",
    "num_samples_to_remove"
   ],
   "id": "4302599d121d76bd",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "86868"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-20T13:15:55.707960Z",
     "start_time": "2024-09-20T13:15:55.677297Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 随机删除label为0的样本\n",
    "train_data = train_data[train_data['label'] != 0]._append(train_data[train_data['label'] == 0].sample(n=num_samples_to_remove))\n",
    "\n",
    "# 重新统计label列中各个标签的出现次数\n",
    "label_counts = train_data['label'].value_counts()\n",
    "print(label_counts)"
   ],
   "id": "7c9d9174994236e2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "label\n",
      "0.0    86868\n",
      "1.0    15838\n",
      "Name: count, dtype: int64\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-20T13:16:11.327656Z",
     "start_time": "2024-09-20T13:16:11.324168Z"
    }
   },
   "cell_type": "code",
   "source": "train_data.shape",
   "id": "2c7d794b4e5acbd5",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(102706, 23)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "模型构建",
   "id": "bb159a5ecf0413b2"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-20T13:26:41.797956Z",
     "start_time": "2024-09-20T13:26:41.788416Z"
    }
   },
   "cell_type": "code",
   "source": "from sklearn.feature_selection import SelectKBest",
   "id": "1a54e2faaf5270f6",
   "outputs": [],
   "execution_count": 24
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-20T13:18:38.904884Z",
     "start_time": "2024-09-20T13:18:38.901520Z"
    }
   },
   "cell_type": "code",
   "source": [
    "label = train_data['label']\n",
    "del train_data['label']"
   ],
   "id": "f46a1b94cbe2a4e9",
   "outputs": [],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-20T13:26:43.973819Z",
     "start_time": "2024-09-20T13:26:43.914997Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.feature_selection import chi2\n",
    "\n",
    "X_new = SelectKBest(chi2, k=15).fit_transform(train_data, label)"
   ],
   "id": "c689ce1ac99684a1",
   "outputs": [],
   "execution_count": 25
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-20T13:26:45.740756Z",
     "start_time": "2024-09-20T13:26:45.717961Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.preprocessing import StandardScaler\n",
    "\n",
    "stdScaler = StandardScaler()#创建一个StandardScaler对象，用于标准化数据。\n",
    "X = stdScaler.fit_transform(X_new)#使用StandardScaler对象对train_data_1进行标准化处理。"
   ],
   "id": "2f3a19edeb3a12d",
   "outputs": [],
   "execution_count": 26
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-20T13:21:39.558594Z",
     "start_time": "2024-09-20T13:21:39.555010Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.metrics import roc_auc_score, roc_curve\n",
    "\n",
    "\n",
    "def model_clf(model):\n",
    "    model.fit(X_train, y_train)  # 训练模型\n",
    "    y_train_pred = model.predict_proba(X_train)  # 预测训练集的概率\n",
    "    y_train_pred_pos = y_train_pred[:, 1]  # 获取正类的概率\n",
    "\n",
    "    y_test_pred = model.predict_proba(X_test)  # 预测测试集的概率\n",
    "    y_test_pred_pos = y_test_pred[:, 1]  # 获取正类的概率\n",
    "\n",
    "    auc_train = roc_auc_score(y_train, y_train_pred_pos)  # 计算训练集的 AUC 分数\n",
    "    auc_test = roc_auc_score(y_test, y_test_pred_pos)  # 计算测试集的 AUC 分数\n",
    "\n",
    "    print(f\"Train AUC Score {auc_train}\")  # 打印训练集的 AUC 分数\n",
    "    print(f\"Test AUC Score {auc_test}\")  # 打印测试集的 AUC 分数\n",
    "\n",
    "    fpr, tpr, _ = roc_curve(y_test, y_test_pred_pos)  # 绘制 ROC 曲线\n",
    "    return fpr, tpr  # 返回 FPR 和 TPR"
   ],
   "id": "91667127e77a7347",
   "outputs": [],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-20T13:38:30.448659Z",
     "start_time": "2024-09-20T13:38:28.428237Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.ensemble import GradientBoostingClassifier\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "X_train,X_test, y_train, y_test = train_test_split(X, label, random_state=0)\n",
    "clf = GradientBoostingClassifier(n_estimators=12,learning_rate=0.8,max_depth=3,random_state=0) \n",
    "model_clf(clf)"
   ],
   "id": "f713d17102c0933f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train AUC Score 0.6163204961603203\n",
      "Test AUC Score 0.5810549013764189\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(array([0.00000000e+00, 4.61297168e-05, 4.61297168e-05, ...,\n",
       "        9.99769351e-01, 9.99769351e-01, 1.00000000e+00]),\n",
       " array([0.00000000e+00, 0.00000000e+00, 2.50062516e-04, ...,\n",
       "        9.99749937e-01, 1.00000000e+00, 1.00000000e+00]))"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {
    "jupyter": {
     "is_executing": true
    },
    "ExecuteTime": {
     "start_time": "2024-09-20T14:00:27.640148Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.ensemble import GradientBoostingClassifier\n",
    "\n",
    "# 定义模型\n",
    "clf = GradientBoostingClassifier(random_state=0)\n",
    "\n",
    "# 定义参数网格\n",
    "param_grid = {\n",
    "    'n_estimators': [10, 50, 100],\n",
    "    'learning_rate': [0.01, 0.1, 0.5, 1.0],\n",
    "    'max_depth': [3, 4, 5],\n",
    "    'min_samples_split': [2, 5, 10]\n",
    "}\n",
    "\n",
    "# 创建网格搜索对象\n",
    "grid_search = GridSearchCV(estimator=clf, param_grid=param_grid, scoring='roc_auc', cv=5)\n",
    "\n",
    "# 进行网格搜索\n",
    "grid_search.fit(X_train, y_train) \n",
    "\n",
    "# 输出最佳参数和最佳分数\n",
    "print(\"Best parameters:\", grid_search.best_params_)\n",
    "print(\"Best cross-validation score:\", grid_search.best_score_)"
   ],
   "id": "beaf59f2ae5f021f",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
